function [ baselinedData ] = TFC1b_ApplyBaseline( data, times, conditiontype, baselineSettings )
%TFC1B_APPLYBASELINE Applies a baseline correction to the data
%  Conditiontype must correspond to something in baselineSettings, either
%  'Response' or 'Stimulus'.

% Make sure we have a default..
if ~exist('baselineSettings', 'var')
    baselineSettings.Stimulus.MinTime = -750;
    baselineSettings.Stimulus.MaxTime = -250;
    baselineSettings.Response.MinTime = 1500;
    baselineSettings.Response.MaxTime = 2000;
end

intervalMin = baselineSettings.(conditiontype).MinTime;
intervalMax = baselineSettings.(conditiontype).MaxTime;
baselineTf = (times>intervalMin & times<intervalMax);

baseline = mean(data(:,baselineTf,:),2); 
baselinedData = data ./ repmat( baseline, [1,size(data,2),1]);

end

















% 
% % PREVIOUS IMPLEMENTATION
% function [ data ] = CorrectBaseline( data, isStimulus )
% 
% % dabs = abs(data.tf); %TODO waarom hier de absolute waarde nemen? Zou niet nodig moeten zijn?
% 
% if isStimulus
%     baselineTf = (data.times>-750 & data.times<-250);
% %     baselineTf = (data.times>-500 & data.times<0);    
% else
%     baselineTf = (data.times>1500 & data.times<2000);
% %     baselineTf = (data.times>-1750 & data.times<-1250);
% %     baselineTf = (data.times>-250 & data.times<250);
% end
% data.baselinePerTrial = mean(data.powerAvg(:,baselineTf,:),2);
% 
% data.powerCorrected = data.powerAvg ./ repmat( data.baselinePerTrial, [1,size(data.powerAvg,2),1]);
% % data.powerCorrected = data.powerAvg - repmat( data.baselinePerTrial, [1,size(data.powerAvg,2),1]);
% data = rmfield(data, 'powerAvg');
% 
% end